package fiszki.namespace;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageButton;

public class AddFiszkaActivity extends Activity {
	private EditText question;
	private EditText answer;
	private ImageButton saveFiszka;
    private ImageButton cancel;
    private int setID;
    
    private FiszkiDBAdapter fiszkiDBAdapter;
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().getAttributes().format = android.graphics.PixelFormat.RGBA_8888;
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.add_new_card);

        openNewConnection();
        initUiElements();
        initButtonsOnClickListeners(); 
	}
	
	// OK
	private void initUiElements() {
		saveFiszka = (ImageButton) findViewById(R.id.buttonSaveSet);
		cancel = (ImageButton) findViewById(R.id.buttonCancel);
		question = (EditText) findViewById(R.id.question);
		answer = (EditText) findViewById(R.id.answer);
		setID = getIntent().getExtras().getInt("setID");
 	}
    
    private void initButtonsOnClickListeners() { 	 	
    	saveFiszka.setOnClickListener(new View.OnClickListener() {
            
			@Override
			public void onClick(View v) {
            	try {
            		saveNewFiszka();        	
            		finish();
            		Bundle ID = new Bundle();
            		ID.putInt("setID", setID);
            		Intent newIntent = new Intent(AddFiszkaActivity.this, SetContentActivity.class);
            		newIntent.putExtras(ID);
            		startActivityForResult(newIntent, 0);
            	}
            	catch(Exception e) {
            	}
            }
        });
    	
    	cancel.setOnClickListener(new View.OnClickListener() {
            
			@Override
			public void onClick(View v) {
            	finish();
            }
        });
	}
    
    private void openNewConnection() {
    	fiszkiDBAdapter = new FiszkiDBAdapter(getApplicationContext());
    	fiszkiDBAdapter.open();
    }
    
    private void saveNewFiszka() throws Exception {
    	
        String setQuestion = question.getText().toString();
        String setAnswer = answer.getText().toString();
        
        if(setQuestion.equals("")) {
        	question.setEms(R.string.string_cannot_be_empty);
        	throw new Exception();
        }
        if(setAnswer.equals("")) {
        	answer.setEms(R.string.string_cannot_be_empty);
        	throw new Exception();
        }
        
        fiszkiDBAdapter.addFiszka(setID, setQuestion, setAnswer);    
    }
    
    // OK
    @Override
    protected void onDestroy() {
        if(fiszkiDBAdapter != null)
        	fiszkiDBAdapter.close();
        
        super.onDestroy();
    }
}
